package com.hzya.frame.utils.core.tree;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzya.frame.utils.core.tree.impl.EasyuiTreeNodeGenerator;

import java.io.PrintStream;

public class TreeNodeGeneratorFactory
{
  private static ITreeNodeGenerator iTreeNodeGenerator;

  public static <T> T createTreeNode(BaseTreeNodeDomain baseTreeNodeDomain, Class generatorClass, Class<T> treeNodeType)
  {
    try
    {
      iTreeNodeGenerator = (ITreeNodeGenerator)generatorClass.newInstance();
    }
    catch (InstantiationException e) {
      e.printStackTrace();
    }
    catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return (T) iTreeNodeGenerator.createTreeNode(baseTreeNodeDomain);
  }

  public static void main(String[] args) throws JsonProcessingException {
    BaseTreeNodeDomain baseTreeNodeDomain = new BaseTreeNodeDomain();

    baseTreeNodeDomain.setId("1");
    baseTreeNodeDomain.setName("测试");
    baseTreeNodeDomain.addAttributes("code", Integer.valueOf(123));
    TreeNode treeNode = (TreeNode)createTreeNode(baseTreeNodeDomain, EasyuiTreeNodeGenerator.class, TreeNode.class);
    ObjectMapper mapper = new ObjectMapper();
    System.out.println(mapper.writeValueAsString(treeNode));
  }
}